package com.example.yangyinzhaixian.AllActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.yangyinzhaixian.Bean.Recordbean;
import com.example.yangyinzhaixian.Customview.MLImageView;
import com.example.yangyinzhaixian.Fragemt.record.BlankFragment1;
import com.example.yangyinzhaixian.Fragemt.record.BlankFragment2;
import com.example.yangyinzhaixian.Fragemt.record.BlankFragment3;
import com.example.yangyinzhaixian.R;
import com.example.yangyinzhaixian.uitl.OkHttpClientManager;
import com.example.yangyinzhaixian.uitl.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.Call;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener {

    private Button login_blank_btn;
    private TextView shezhi_text;
    private ImageView record_image;
    private MLImageView hand_image;
    private TextView teacher_name;
    private TextView teacher_title;
    private LinearLayout record_linear1;
    private TextView record_roomTitle;
    private RelativeLayout record_relative;
    private LinearLayout activity_record;
    private String liveId;
    private String price;
    private TextView recoord_price;
    private TabLayout record_FindFragment_title;
    private ArrayList<String> list_title;
    private ArrayList<Fragment> fragments;
    private ViewPager record_viewpager;
    private Button record_btn_try_see;
    private Recordbean recordbean;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_record);
        initView();
        Intent intent = getIntent();
        liveId = intent.getStringExtra("LiveId");
        price = intent.getStringExtra("price");
        String token = (String) SharedPreferencesUtils.getParam(RecordActivity.this, "Token", "");
        Log.i("TagA", "token:" + token);
        OkHttpUtils.get().url("http://api.immedc.com/restapi/live/getLive")
                .addHeader("CH-TOKEN", token)
                .addParams("liveId", liveId)
                .build().execute(new StringCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) throws JSONException {
                Log.i("TagA", response);
                JSONObject jsonObject = new JSONObject(response);
                String success = jsonObject.getString("success");
                Gson gson = new Gson();
                recordbean = gson.fromJson(response, Recordbean.class);
                Recordbean.DataBean data = recordbean.getData();
                //标题
                shezhi_text.setText(data.getCourseTitle());
                //添加图片
                OkHttpClientManager.displayImage(record_image, data.getImageUrl());
                //添加头像
                OkHttpClientManager.displayImage(hand_image, data.getTeacherAvatar());
                //添加文字
                teacher_name.setText(data.getTeacherName());
                teacher_title.setText(data.getTeacherTitle());
                record_roomTitle.setText(data.getShareView().getShareTitle());
                //价格
                if (data.getPrice() == 0) {
                    recoord_price.setText("免费");
                } else {
                    recoord_price.setText(data.getPrice() + "元");
                }
                //为TabLayout添加tab名称
                list_title = new ArrayList<>();
                list_title.add("本课知识");
                list_title.add("往期回顾");
                list_title.add("老师主页");
                record_FindFragment_title.addTab(record_FindFragment_title.newTab().setText(list_title.get(0)));
                record_FindFragment_title.addTab(record_FindFragment_title.newTab().setText(list_title.get(1)));
                record_FindFragment_title.addTab(record_FindFragment_title.newTab().setText(list_title.get(2)));
                fragments = new ArrayList<>();
                fragments.add(new BlankFragment1(recordbean));
                fragments.add(new BlankFragment2(recordbean));
                fragments.add(new BlankFragment3(recordbean));
                Find_tab_Adapter find_tab_adapter = new Find_tab_Adapter(getSupportFragmentManager(), fragments, list_title);
                record_viewpager.setAdapter(find_tab_adapter);
                record_FindFragment_title.setupWithViewPager(record_viewpager);
            }
        });
    }

    private void initView() {
        login_blank_btn = (Button) findViewById(R.id.login_blank_btn);
        shezhi_text = (TextView) findViewById(R.id.shezhi_text);
        record_image = (ImageView) findViewById(R.id.record_image);
        hand_image = (MLImageView) findViewById(R.id.hand_image);
        teacher_name = (TextView) findViewById(R.id.teacher_name);
        teacher_title = (TextView) findViewById(R.id.teacher_title);
        record_linear1 = (LinearLayout) findViewById(R.id.record_linear1);
        record_roomTitle = (TextView) findViewById(R.id.record_roomTitle);
        record_relative = (RelativeLayout) findViewById(R.id.record_relative);
        activity_record = (LinearLayout) findViewById(R.id.activity_record);
        login_blank_btn.setOnClickListener(this);
        recoord_price = (TextView) findViewById(R.id.recoord_price);
        recoord_price.setOnClickListener(this);
        record_FindFragment_title = (TabLayout) findViewById(R.id.record_FindFragment_title);
        record_FindFragment_title.setTabMode(TabLayout.MODE_FIXED);
        record_viewpager = (ViewPager) findViewById(R.id.record_viewpager);
        record_btn_try_see = (Button) findViewById(R.id.record_btn_try_see);
        record_btn_try_see.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_blank_btn:
                break;
            case R.id.record_btn_try_see:
                Intent intent = new Intent(this,PlayActivity.class);
                intent.putExtra("videoUrl",recordbean.getData().getVideoUrl());
                startActivity(intent);
                break;
        }
    }

    class Find_tab_Adapter extends FragmentPagerAdapter {
        private ArrayList<String> list_title;
        private ArrayList<Fragment> fragments;

        public Find_tab_Adapter(FragmentManager fm, ArrayList<Fragment> fragments, ArrayList<String> list_title) {
            super(fm);
            this.fragments = fragments;
            this.list_title = list_title;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return list_title.size();
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {
            return list_title.get(position % list_title.size());
        }
    }
}
